package com.xu.common.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.health.PackageHealthStats;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by 12708 on 2018/5/27.
 */

public abstract class Fragment extends android.support.v4.app.Fragment {


    /**
     * UI
     */
    private View mRoot = null;

    /**
     * data
     *
     * @param context
     */
    private Unbinder mUnbinder = null;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        initArgs(getArguments());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mRoot == null) {
            int layoutId = getContentLayoutId();
            //设置布局，容器，是否依覆父容器
            View root = inflater.inflate(layoutId, container, false);
            initWidget(root);
            mRoot = root;
        } else {
            if (mRoot.getParent() != null) {
                ((ViewGroup) mRoot.getParent()).removeView(mRoot);
            }
        }

        return mRoot;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData(mRoot);
    }

    /**
     * 初始化数据
     *
     * @param root
     */
    public void initData(View root) {

    }

    /**
     * UI控件初始化
     *
     * @param root
     */
    public void initWidget(View root) {
        mUnbinder = ButterKnife.bind(this, root);
    }

    /**
     * Bundle初始化相关参数
     *
     * @param bundle
     * @return
     */
    public void initArgs(Bundle bundle) {


    }

    //是否拦截按键式返回键
    public boolean onBackPress() {
        return false;

    }

    /**
     * UI封装
     *
     * @return
     */
    protected abstract int getContentLayoutId();
}
